// pages/varietyDetail/varietyDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    commodityList:[],
    more:false,
    key:0,
    id:0,
    selectList:[
      {
        name:"全部"
      },
      {
        name:"半球网络摄像机"
      },
      {
        name:"枪式网络摄像机"
      },
      {
        name:"网络红外一体机"
      },
      {
        name:"摄像机配套"
      }
    ],

    
  },
  // 获取数据
  request:function(){
    var that=this;
    wx.request({
      url: 'https://www.xiaozheng8.com/shop/product/findByCateParentId',
      method:"post",
      data:{
        cateParentId:that.data.id
      },
      success:function(res){
        console.log(res);
        that.setData({
          commodityList:res.data.data
        })
      }
    }),
    wx.request({
      url: 'https://www.xiaozheng8.com/shop/cate/findAll',
      data: '',
      method: 'post',
      success: function(res) {
        console.log(res);
      for(var i=0;i<res.data.data.length;i++){
        if(res.data.data[i].id==that.data.id){
          that.setData({
            selectList:res.data.data[i].smallCates
          })
          var arr=[];
          var obj={name:"全部"};
          that.data.selectList.unshift(obj);
          console.log(that.data.selectList);
          that.setData({
            selectList: that.data.selectList
          })
        }
      }
      console.log(that.data.selectList);
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  // 进入商品详情页
  detail:function(e){
    var id=e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
      url: '../commodityDetail/commodityDetail?productId='+id,
    })
  },
  // 选择分类
  item:function(e){
    var that=this;
    var index=e.currentTarget.dataset.index;
    var cateId=e.currentTarget.dataset.cateid;
    console.log(cateId);
    if(cateId==undefined){
      wx.request({
        url: 'https://www.xiaozheng8.com/shop/product/findAll',
        method: "post",
        data: {},
        success: function (res) {
          console.log(res);
          that.setData({
            commodityList: res.data.data
          })
        }
      })
    }else{
      console.log(111)
      wx.request({
        url: 'https://www.xiaozheng8.com/shop/product/findByCateId',
        data: {
          cateId:cateId,
          parentId:that.data.id
        },
        method: 'post',
        success: function(res) {
          console.log(res);
          that.setData({
            commodityList: res.data.data
          })
        },
        fail: function(res) {},
        complete: function(res) {},
      })
    }
    this.setData({
      key:index
    })
  },
  // 点击更多
  more:function(){
    var that=this;
    this.setData({
      more:!that.data.more
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.setData({
      id:options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.request();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})